//
//  members_A0.m
//  K-Music
//
//  Created by CHUN on 16/1/15.
//  Copyright © 2016年 CHUN. All rights reserved.
//

#import "members_A0.h"
#import "member_Model.h"


@interface members_A0 ()<UITableViewDataSource,UITableViewDelegate>

@property (nonatomic ,strong) NSMutableArray * membersDatas;
@property (nonatomic ,strong) UITableView *members_A0_TableView;

@end

@implementation members_A0

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.hidden = YES;
    self.view.backgroundColor = RGB(241, 241, 241);
    self.navigationItem.title = @"会员中心";
    
    self.navigationItem.leftBarButtonItem = [UIBarButtonItem itemWithIcon:@"back" highIcon:@"back" target:self action:@selector(back_back)];
    
    [self creatUI];
}
- (void)back_back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -creatUI-
- (void)creatUI
{
    self.members_A0_TableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT + 44) style:UITableViewStylePlain];
    self.members_A0_TableView.delegate = self;
    self.members_A0_TableView.dataSource =self;
    self.members_A0_TableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.members_A0_TableView.backgroundColor = RGB(241, 241, 241);
    [self.view addSubview:self.members_A0_TableView];
    
    [self get_chat_group];
}

- (void)get_chat_group
{
    [MBProgressHUD showWithStatus:@"加载中..." toView:self.view];

    AFHTTPRequestOperationManager * manager = [AFHTTPRequestOperationManager manager];
    
    NSString*infoStr11=[NSString stringWithFormat:@"/api/member?page=0&size=20&sort=createdDate,DESC"];
    
    NSString *intactURL = [MyBaseUrl stringByAppendingString:infoStr11];
    
    NSString * token = [FileStorage NSUserDefault_getObjectForKey:access_token];
    
    //    创建请求
    manager.requestSerializer =[AFHTTPRequestSerializer serializer];
    
    [manager.requestSerializer setValue:@"application/json"
                     forHTTPHeaderField:@"Accept"];
    [manager.requestSerializer setValue:[NSString stringWithFormat: @"Bearer %@",token]
                     forHTTPHeaderField:@"Authorization"];
    
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    [manager GET:intactURL parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        
        NSDictionary * dic = [NSJSONSerialization JSONObjectWithData:responseObject options:0 error:nil];
        
        NSLog(@"成功GET --> %@", dic);
        _membersDatas = [NSMutableArray array];
        NSArray * arr = dic[@"body"][@"content"];
        NSArray * bagDatas = [member_Model objectArrayWithKeyValuesArray:arr];
        _membersDatas = [NSMutableArray arrayWithArray:bagDatas];
        [self.members_A0_TableView reloadData];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
        NSLog(@"%@",error);
        
        [MBProgressHUD showErrorWithStatus:@"数据获取失败" toView:self.view];
        
    }];
}


#pragma mark -tableviewDatasource-
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString * cellID = @"cellID";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    UILabel * moonLabel;
    UILabel * moneyLabel;
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
        moonLabel = [[UILabel alloc] init];
        moonLabel.font = [UIFont systemFontOfSize:15.0f];
        [cell.contentView addSubview:moonLabel];
        
        moneyLabel = [[UILabel alloc] init];
        moneyLabel.font = [UIFont systemFontOfSize:15.0f];
        moneyLabel.textAlignment = NSTextAlignmentCenter;
        moneyLabel.textColor = [UIColor whiteColor];
        moneyLabel.backgroundColor = RGB(244, 219, 182);
        [cell.contentView addSubview:moneyLabel];
        
        [moonLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.left.equalTo(cell.contentView.mas_left).with.offset(15);
        }];
        
        [moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(cell.contentView);
            make.width.mas_equalTo(@(80));
            make.height.mas_equalTo(@(35));
            make.right.equalTo(cell.contentView.mas_right).with.offset(-15);
        }];
    }
    
    member_Model * data = [self.membersDatas objectAtIndex:indexPath.row];
    moonLabel.text = data.descriptionD;
    moneyLabel.text = data.price;
    
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.membersDatas.count;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
     [tableView deselectRowAtIndexPath:tableView.indexPathForSelectedRow animated:YES];
}

@end
